<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaCommonJsApiController extends ManageBaseController{
    private function get_employee_in_dept ($deptId) {
        $map['dept_id'] = $deptId;
        $employees = M('OaEmployee')->where($map)->select();
        $data = array();
        foreach ($employees as $employee) {
            array_push($data,array('id'=>$employee['id'],'name'=>$employee['name']));
        }
        $sub_depts = M('OaDept')->where(array('pid'=>$deptId))->select();
        foreach ($sub_depts as $sub_dept) {
            $tmp = $this->get_employee_in_dept($sub_dept['id']);
            $data = array_merge($data,$tmp);
        }
        return $data;
    }
    public function getEmployeeInDept () {
        $map['dept_id'] = I('dept_id');
        $employees = M('OaEmployee')->where($map)->select();
        $data = array();
        foreach ($employees as $employee) {
            array_push($data,array('id'=>$employee['id'],'name'=>$employee['name']));
        }
        $sub_depts = M('OaDept')->where(array('pid'=>I('dept_id')))->select();
        foreach ($sub_depts as $sub_dept) {
            $tmp = $this->get_employee_in_dept($sub_dept['id']);
            $data = array_merge($data,$tmp);
        }
        $this->ajaxReturn($data,'JSON');
    }

    private function get_subDepts($pdeptId, $deep, &$deptsDate) {
        $map['pid'] = $pdeptId;
        $pdept = M('OaDept')->where(['id'=>$pdeptId])->find();
        $depts = M('OaDept')->where($map)->select();
        $tmp_deep = $deep + 1;
        if($tmp_deep > 2) $tmpStr = $pdept['name'].'_';

//        for ($i = 0; $i < $deep; $i++){
//            $tmpStr = $tmpStr.'-';
//        }

        foreach ($depts as $dept) {
            array_push($deptsDate,array('id'=>$dept['id'],'name'=>($tmpStr.$dept['name'])));
            $this->get_subDepts($dept['id'], $tmp_deep, $deptsDate);
        }
    }
    public function getDeptsSelectList () {
        $data = array();
        $this->get_subDepts(0,0,$data);
        $this->ajaxReturn($data,'JSON');
    }

}

